package com.pkuse.bjousystem.controller;

import com.pkuse.bjousystem.dto.student.*;
import com.pkuse.bjousystem.repository.student.StudentRepository;
import com.pkuse.bjousystem.repository.student.ZongChengJiRepository;
import com.pkuse.bjousystem.utils.DateUtils;
import com.pkuse.bjousystem.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/student/api/v1")
public class StudentController {

    @Autowired
    StudentRepository studentRepository;

    @Autowired
    ZongChengJiRepository zongChengJiRepository;

    @GetMapping("/check")
    public String check() {
        return "ok";
    }

    @RequestMapping("/status/{studentID}")
    public XueShengXinXiDTO getStudentStatus(@PathVariable("studentID") String studentID) {
        XueShengXinXiDTO studentDetials = studentRepository.findStudentDetials(studentID);
        return studentDetials;
    }

    @RequestMapping("/score/current/{studentID}")
    public Page<XueShengChengJiDTO> getStudentResults(@PathVariable("studentID") String studentID, @RequestBody Map<String, String> body) {
        return zongChengJiRepository.listXueShengChengJi(studentID, DateUtils.thisYear(), PageUtils.fromRequest(body));
    }

    @RequestMapping("/score/history/{studentID}")
    public Page<XueShengChengJiDTO> getStudentResultsHistory(@PathVariable("studentID") String studentID, @RequestBody Map<String, String> body) {
        return zongChengJiRepository.listWangNianXueShengChengJi(studentID, DateUtils.thisYear(), PageUtils.fromRequest(body));
    }

    @RequestMapping("/plan/brief/{studentID}")
    public XueShengJiHuaJianYaoDTO getStudentPlanBreif(@PathVariable("studentID") String studentID) {
        return studentRepository.getStudentPlanBreif(studentID);
    }

    @RequestMapping("/plan/details/{studentID}")
    public XueShengJiHuaXiangQingCollectionDTO listStudentPlanDetails(@PathVariable("studentID") String studentID) {
        List<XueShengJiHuaXiangQingDTO> xueShengJiHuaXiangQingDTOS = zongChengJiRepository.listJiHuaXiangQing(studentID);

        Map<String, XueShengJiHuaXiangQingDTO> filteredXueShengJiHuaXiangQingDTOSMap = new HashMap<>();
        for (XueShengJiHuaXiangQingDTO xueShengJiHuaXiangQingDTO : xueShengJiHuaXiangQingDTOS) {
            String kcid = xueShengJiHuaXiangQingDTO.getKcid();
            if (filteredXueShengJiHuaXiangQingDTOSMap.get(kcid) == null) {
                filteredXueShengJiHuaXiangQingDTOSMap.put(kcid, xueShengJiHuaXiangQingDTO);
            } else {
                int stored = filteredXueShengJiHuaXiangQingDTOSMap.get(kcid).getZhcjNumber();
                int current = xueShengJiHuaXiangQingDTO.getZhcjNumber();
                if (stored < current) {
                    filteredXueShengJiHuaXiangQingDTOSMap.put(kcid, xueShengJiHuaXiangQingDTO);
                }
            }
        }

        List<XueShengJiHuaXiangQingDTO> filteredXueShengJiHuaXiangQingDTOS = new ArrayList<>(filteredXueShengJiHuaXiangQingDTOSMap.values());


        Map<String, Double> z = new HashMap<>();
        for (XueShengJiHuaXiangQingDTO xueShengJiHuaXiangQingDTO : filteredXueShengJiHuaXiangQingDTOS) {
            String mkh = xueShengJiHuaXiangQingDTO.getMkh();
            double xf = xueShengJiHuaXiangQingDTO.getXfNumber();
            if (xueShengJiHuaXiangQingDTO.getZhcjNumber() >= 60) {
                if (z.get(mkh) == null) {
                    z.put(mkh, xf);
                } else {
                    z.put(mkh, z.get(mkh) + xf);
                }
            }
        }

        final double zxf = z.values().stream().reduce(0.0, (x, y) -> x + y);

        List<XueShengJiHuaXiangQingDTO> collect = filteredXueShengJiHuaXiangQingDTOS.stream()
                .sorted(Comparator.comparing(XueShengJiHuaXiangQingDTO::getMkh))
                .map(x -> {
                    Double tgzf = z.get(x.getMkh());
                    x.setTgzf(tgzf == null ? "" : String.valueOf(tgzf));
                    x.setZxf(zxf);
                    return x;
                }).collect(Collectors.toList());


        return new XueShengJiHuaXiangQingCollectionDTO(collect);
    }

}
